<?php

namespace app\shopapi\controller;

use app\admin\model\shop\Category;
use app\common\controller\Shopapi;
use app\shopapi\model\pay\Alipay;
use app\shopapi\model\pay\ShopInfeed;
use app\shopapi\model\pay\Wechat;
use app\shopapi\model\Shop;
use app\shopapi\model\ShopAdmin;
use app\shopapi\model\ShopUser;

/**
 * 店铺认证相关接口
 */
class Auth1 extends Shopapi
{
    protected $noNeedLogin = ['contracturl'];
    protected $noNeedRight = '*';


    //图片上传
    public function upload()
    {
        $file = input('file','');
        $ext = input('ext','');
        $model = new ShopInfeed();
        $res = $model->upload($file,$ext);
        $this->success('',$res);
    }

    //根据区的adcode获取省市adcode
    public function getAdcode()
    {
        $adcode = input('adcode','');
        $model = new \app\shopapi\model\Shop();
        $res = $model->getProvinceCode($adcode);
        $this->success('',$res);
    }

    //银行列表
    public function getBank()
    {
        $model = new \app\shopapi\model\Bank();
        $res = $model->getlist();
        $this->success('',$res);
    }

    //银行支行列表
    public function bankBranch()
    {
        $bank_name = input('bank_name','');
        $district = input('district','');
        $city = input('city','');
        $model = new \app\shopapi\model\Bank();
        $res = $model->bank($bank_name,$district,$city);
        $this->success('',$res);
    }

    //商户进件
    public function infeed()
    {
        $shop_id = input('shop_id',0);
        try {
            $model = new ShopInfeed();

            $res = $model->apply($shop_id);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('',$res);
    }

    //合同重定向
    public function contracturl()
    {

        $short_url = input('code','');
        $shop_info = Shop::get(['short_url'=>$short_url]);
        $url = $shop_info->contract_url;
        header("location:$url");
    }

    //电子合同
    public function contract()
    {
        $merchant_no = input('merchant_no',0);
        try {
            $model = new ShopInfeed();

            $res = $model->contract($merchant_no);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('',$res);
    }

    //获取微信认证码
    public function wxAuthCode()
    {
        $merchant_no = input('merchant_no',0);
        try {
            $model = new Wechat();

            $res = $model->authcode($merchant_no);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('',$res);
    }

    //查询微信认证状态
    public function wxCodeState()
    {
        $merchant_no = input('merchant_no',0);
        try {
            $model = new Wechat();

            $res = $model->authCodeStatus($merchant_no);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('',$res);
    }

    //撤销微信申请单
    public function recallWx()
    {
        $merchant_no = input('merchant_no',0);
        try {
            $model = new Wechat();

            $res = $model->recall($merchant_no);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('',$res);
    }

    //获取支付宝认证码
    public function aliAuthCode()
    {
        $merchant_no = input('merchant_no',0);
        try {
            $model = new Alipay();

            $res = $model->authcode($merchant_no);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('',$res);
    }

    //查询支付宝认证状态
    public function aliCodeState()
    {
        $merchant_no = input('merchant_no',0);
        try {
            $model = new Alipay();

            $res = $model->authCodeStatus($merchant_no);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('',$res);
    }

    //撤销支付宝申请单
    public function recallAli()
    {
        $merchant_no = input('merchant_no',0);
        try {
            $model = new Alipay();

            $res = $model->recall($merchant_no);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('',$res);
    }
}